<?php
namespace Macaca\Controller\Admin;

use Macaca;
use Macaca\Module\Admin\User as ModuleAdminUser;
use Macaca\View\Admin as View;
use Monkey;

class User extends Base {

    /**
     * @return ModuleAdminUser
     */
    public function moduleUser() {
        return $this->moduleAdminUser;
    }

    public function actionLogout() {
        $this->moduleUser()->logout();
        $this->redirect($this->app->INDEX_ROOT_URL);
    }

    public function actionGetList() {
        $app = $this->app;
        $view = new View\User($app);
        $module = $this->moduleUser();
        $linker = $this->linker;

        $data['title'] = 'Macaca 管理员管理';
        $data['url_add'] = $linker->toFix('/admin/user/add');
        $data['url_edit'] = $linker->to('/admin/user/edit');
        $data['url_delete'] = $linker->to('/admin/user/delete');

        $page = $linker->router->getParameter('page', 0);
        $pageUrlPre = $linker->to('/admin/user/');
        $itemCount = $module->getCount();
        $data['page_box'] =
            $view->view->page()
                ->setUrlPre($pageUrlPre)->setCurrentLayout('first-pre-list-next-last-total')
                ->getByList($page, $itemCount, 10);

        $data['user'] = $module->getUserList($page, 10);
        $this->response->setBody($view->render($data));
    }

    public function actionAddItem() {
        $app = $this->app;
        $view = new View\UserEditor($app);
        $module = $this->moduleUser();

        $data['title'] = '添加管理员';
        $data['url_save'] = $this->linker->toFix('/admin/user/save');

        $data['user'] = $module->getEmptyItem();

        $this->response->setBody($view->render($data));
    }

    public function actionGetItem() {
        $uId = $this->app->router()->getParameter('uid');
        $user = $this->moduleUser()->getItemById($uId);
        $this->showItem($user);
    }

    public function actionGetMe() {
        $user = $this->moduleUser();
        $user = $user->getItemById($user->getFieldFromSession($user->modelPriKey));
        $this->showItem($user);
    }

    protected function showItem($user) {
        if (empty($user)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;你要编辑的管理员不存在！';
            return;
        }
        $data['title'] = '编辑管理员';
        $data['url_save'] = $this->linker->toFix('/admin/user/save');
        $data['user'] = $user;
        $view = new View\UserEditor($this->app);
        $this->response->setBody($view->render($data));
    }

    public function actionDeleteItem() {
        $module = $this->moduleUser();
        $nid = $this->app->router()->getParameter('uid');

        if ($module->deleteItemById($nid)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;成功删除所选管理员。';
        }
        else {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;管理员删除失败!';
        }
    }

    public function actionSaveItem() {
        $module = $this->moduleUser();

        if ($module->saveItem($_POST)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;管理员保存成功!';
        }
        else {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;管理员保存失败!';
        }

    }

} 